package sv.gob.mtps.sib.controller;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import sv.gob.mtps.sib.model.Factura;
import sv.gob.mtps.sib.model.Objetoespecifico;
import sv.gob.mtps.sib.model.ProductoHasObjetoespecifico;
import sv.gob.mtps.sib.model.VistaTotalOe;
import sv.gob.mtps.sib.service.ObjetoespService;
import sv.gob.mtps.sib.service.ProductoService;

@Controller
public class ObjetoespController {

	@Autowired
	private ObjetoespService objetoespService;
	
	@Autowired
	private ProductoService productoService;

	@RequestMapping("/admin/errOE")
	public String err(Map<String, Object> map,
			@ModelAttribute("oe") @Valid Objetoespecifico oe,
			BindingResult result) {
		map.put("oe", oe);
		map.put("oeList", objetoespService.listOE());
		return "/admin/showOE";
	}

	@RequestMapping("/admin/showOE")
	public String listOE(Map<String, Object> map) {
		map.put("oe", new Objetoespecifico());
		map.put("oeList", objetoespService.listOE());
		return "/admin/oe";
	}

	@RequestMapping(value = "/admin/addOE", method = RequestMethod.POST)
	public String addOE(
			@ModelAttribute("oe") @Valid Objetoespecifico oe,
			BindingResult result) {
		if (!result.hasErrors()) {
			try{
				objetoespService.addOE(oe);
			}
			catch(DataAccessException ex){
				ex.printStackTrace();
			}
			catch(Exception ex){
				ex.printStackTrace();
			}
			
			return "redirect:/admin/showOE";
		} else {
			return "redirect:/admin/errOE";
		}

	}

	@RequestMapping(value = "/admin/editOE", method = RequestMethod.POST)
	public String updateOE(
			@ModelAttribute("oe") @Valid Objetoespecifico oe,
			BindingResult result) {
		if (!result.hasErrors()) {
			try{
				objetoespService.updateOE(oe);
			}
			catch(DataAccessException ex){
				ex.printStackTrace();
			}
			catch(Exception ex){
				ex.printStackTrace();
			}


			return "redirect:/admin/showOE";
		} else {
			return "redirect:/admin/errOE";
		}

	}

	@RequestMapping(value = "/admin/deleteOE", method = RequestMethod.POST)
	public String deleteOE(
			@ModelAttribute("oe") Objetoespecifico oe,
			BindingResult result) {
				try{
					objetoespService.removeOE(oe.getIdObjetoespecifico());
				}
				catch(DataAccessException ex){
					ex.printStackTrace();
				}
				catch(Exception ex){
					ex.printStackTrace();
				}	
			
		return "redirect:/admin/showOE";
	}
	
	@RequestMapping(value = "/admin/showPOE", method = RequestMethod.POST)
	public ModelAndView showPOE(@ModelAttribute("oe") Objetoespecifico oe, BindingResult result, ModelMap map) {		
		ProductoHasObjetoespecifico poe = new ProductoHasObjetoespecifico();		
		poe.setObjetoespecifico(oe);					
		map.put("poe", poe);		
		map.put("poeList",  productoService.listObjPr(oe.getIdObjetoespecifico()));		
		System.out.println(poe.getObjetoespecifico().getNombreObjetoespecifico());
		System.out.println("se obtiene el objeto especifico " + oe.getIdObjetoespecifico());
		return new ModelAndView("/admin/agregarProducto_OE",map);
	}
	
	/*se muestra Detalle*/	
	
	@RequestMapping(value = "/admin/showPOEDET")
	public  String showPOEDET(@ModelAttribute("poe") ProductoHasObjetoespecifico poe,BindingResult result,Map<String, Object> map, HttpServletRequest request){
		try{
			String oeId = (String) request.getParameter("oeId");
			System.out.println("tengo..." + oeId);
			
			//ProductoHasObjetoespecifico poee = new ProductoHasObjetoespecifico();
			Objetoespecifico objetoespecifico = objetoespService.findOE(Integer.valueOf(oeId));
			poe.setObjetoespecifico(objetoespecifico);
			
			System.out.println("recibo..."+ poe);
			System.out.println("recibo..."+ poe.getObjetoespecifico().getNombreObjetoespecifico());
						
			
			/*Objetoespecifico id  = (Objetoespecifico) request.getAttribute("foo"); 
			
			System.out.println("ingreso al metodo..."+ id);*/
			
			map.put("poe", poe);
			map.put("poeList",  productoService.listObjPr(poe.getObjetoespecifico().getIdObjetoespecifico()));
			
			
			//System.out.println("ingreso al metodo"+ poe.getObjetoespecifico().getIdObjetoespecifico());
			
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
		//map.put("poeList",  productoService.listObjPr(poe.getObjetoespecifico().getIdObjetoespecifico()));
		return "/admin/agregarProducto_OE";		
	}
	
	
	@RequestMapping(value = "/admin/addPOE", method = RequestMethod.POST)
	public ModelAndView addPOE(@ModelAttribute("poe") ProductoHasObjetoespecifico poe, BindingResult result, ModelMap map){
		
		/*System.out.println(poe.getObjetoespecifico().getIdObjetoespecifico());		
		Objetoespecifico oe = poe.getObjetoespecifico();		
		System.out.println(oe.getIdObjetoespecifico());*/
		Objetoespecifico oe = objetoespService.findOE(poe.getObjetoespecifico().getIdObjetoespecifico());
		int total = 0;
		VistaTotalOe dto = null;
		try{
			if(objetoespService.pobjExist(oe.getIdObjetoespecifico(), poe.getProducto().getIdProducto()).isEmpty()){
				List datos =objetoespService.listTotalObj(oe.getIdObjetoespecifico());
				if(!datos.isEmpty()){
					Iterator it = datos.iterator();
					while(it.hasNext()){
						total = 0;
						dto = (VistaTotalOe)it.next();
						total = Integer.parseInt(dto.getId().getTotalOe().toString());
						total =total+1;
						
					}
				}
				else{
					total = 1;
				}
				System.out.println("Total " +total);
				
				System.out.println("Total " +oe.getIdObjetoespecifico());
				System.out.println("Numero Producto " + oe.getNumeroObjetoespecifico() + ""+total);
				poe.setNumeroProducto(oe.getNumeroObjetoespecifico() + ""+total);
				objetoespService.addPOE(poe);
			}
			
		}
		catch(DataAccessException ex){
			ex.printStackTrace();
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
		map.put("poe", poe);
		map.put("poeList",  productoService.listObjPr(poe.getObjetoespecifico().getIdObjetoespecifico()));
		return new ModelAndView("/admin/agregarProducto_OE",map);
	}
	
	@RequestMapping(value = "/admin/editPOE", method = RequestMethod.POST)
	public ModelAndView updatePOE(@ModelAttribute("poe") ProductoHasObjetoespecifico poe, BindingResult result, ModelMap map){
		System.out.println(poe.getObjetoespecifico());
		
		map.put("poe", poe);
		
		try{
			objetoespService.updatePOE(poe);
		}
		catch(DataAccessException ex){
			ex.printStackTrace();
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
		map.put("poeList",  productoService.listObjPr(poe.getObjetoespecifico().getIdObjetoespecifico()));
		map.put("productoList", productoService.listProducto());
		return new ModelAndView("/admin/agregarProducto_OE",map);
	}
	
	@RequestMapping(value = "/admin/deletePOE", method = RequestMethod.POST)
	public ModelAndView deletePOE(@ModelAttribute("poe") ProductoHasObjetoespecifico poe, BindingResult result, ModelMap map){
		System.out.println(poe.getObjetoespecifico());
		map.put("poe", poe);
		
		try{
			objetoespService.removePOE(poe.getIdProductoHasObjetoespecifico());
		}
		catch(DataAccessException ex){
			ex.printStackTrace();
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
		map.put("poeList",  productoService.listObjPr(poe.getObjetoespecifico().getIdObjetoespecifico()));
		map.put("productoList", productoService.listProducto());
		return new ModelAndView("/admin/agregarProducto_OE",map);
	}
	
	
	
	@RequestMapping(value = "/admin/findObj")
	public  String findObj(@ModelAttribute("factura") Factura factura,BindingResult result,Map<String, Object> map){
		map.put("productoList", productoService.listProductoWv());
		return "admin/find_PRO";		
	}
	
}
